<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/16 0016
 * Time: 2:28
 */

namespace api\storage\controller;


use api\storage\common\Constant;
use api\storage\service\SmtReturnService;
use service\logger\LoggerFactory;
use think\Model;
use think\Request;

class SmtReturnController extends AppBaseController
{
    protected $smtReturnService;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->smtReturnService = new SmtReturnService();
    }

    /**
     * @api {post} /api/storage/smt_return/unload 1.机器卸料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName unload
     * @apiGroup  SmtReturn
     * @apiVersion 0.0.1
     * @apiDescription 将物料从机器上卸下
     *
     * @apiParam {string}  reelId  盘料上的reelId
     * @apiParam {int}  returnCount  盘料上的reelId
     * 对扫描的物料进行出库
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 物料不在站位上
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 退料失败
     **/
    public function unload(){
        $reelId = input('reelId');
        $returnCount = input('returnCount');
        $result = $this->smtReturnService->unloadFromMachineSeat($reelId,$returnCount);

        switch ($result['status']){
            case 1:
                $this->error("物料不在站位上");
                break;
            case 2:
                $this->success("退料成功");
                break;
            case 3:
                $this->error('该物料被锁定,可能被选为续料或有续料未上机。',$result['data'],Constant::RETURN_MATERIEL_IS_LOCKED);
                break;
            case 4:
                $this->error('该站位物料未发够，请勿退料。');
                break;
            case 5:
                $this->error('退料数量过大，超过最低可退数量');
                break;
        }
    }

    /**
     * @api {get} /api/storage/storage/smt_return/check_return 2.还料检查
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName check_return
     * @apiGroup  SmtReturn
     * @apiVersion 0.0.1
     * @apiDescription 检查物料信息
     *
     * @apiParam {string}  reelId  盘料上的reelId
     * 对将要入库的物料进行扫描，检查信息是否正确
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 物料不在站位上
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 退料失败
     **/
    public function checkReturn(){
        $reelId = input('reelId');
        $result = $this->smtReturnService->checkReturn($reelId);
        switch ($result['status']){
            case 0:
                $this->error('该物料是准备上机的物料');
                break;
            case 1:
                $this->success("请扫描站位码或容器号",$result['data']);
                break;
            case 2:
                $this->error("该物料是移料物料");
                break;
            case 3:
                $this->error('该物料不存在于中转区中');
                break;
            default:
                $this->error('还料检查有bug');
                break;
        }
}
    /**
     * @api {post} /api/storage/smt_return/smt_input 3.还料入库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName smt_input
     * @apiGroup  SmtReturn
     * @apiVersion 0.0.1
     * @apiDescription  扫描站位，将物料入库
     *
     * @apiParam {string}  reelId  盘料上的reelId
     * 对扫描的物料进行出库
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 物料不在站位上
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 退料失败
     **/

    public function smtInput(){
        $reelId = input('reelId');
        $seatCode = input('seatCode');
        $containerCode = input('containerCode');

        $storageUuid = $this->getStorageUuid();
        $storageUuid = "184928afb9e35f3ff421da05fc3f917f";
        $result = $this->smtReturnService->smtInput($reelId, $seatCode, $containerCode,$storageUuid, $this->getUsername());

        switch ($result['status']){
            case 1:
                $this->success('入库成功');
                break;
            case 2:
                $this->error('该物料不存在');
                break;
            case 3:
                $this->error("该站位不可用");
                break;
            case 4:
                $this->error("该站位已被占用");
                break;
            case 5:
                $this->error('该站位不存在');
                break;
            case 6:
                $this->error('此仓库中，该容器不存在');
                break;
            case 7:
                $this->error('该容器未上架');
                break;
        }

    }
}